package lol.clann.minecraft.springboot.plugin.bukkit.command.resolver.factory;

import com.ruoshui.utils.java.exception.ExceptionUtils;
import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolver;
import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolverFactory;
import lol.clann.minecraft.springboot.bukkit.utils.NMSReflectUtils;
import org.bukkit.command.CommandException;
import org.springframework.stereotype.Component;

/**
 *
 * @author pyz
 * @date 2019/5/6 9:29 PM
 */
@Component
public class ClassCommandResolverFactory implements CommandResolverFactory {
    @Override
    public boolean canResolve(Class<?> type) {
        return Class.class == type;
    }

    @Override
    public CommandResolver getResolver(Class<?> clazz) {
        return (sender, arg, type) -> {
            try {
                return NMSReflectUtils.forName(arg);
            } catch (Throwable e) {
                throw new CommandException(ExceptionUtils.unwrap(e).getMessage());
            }
        };
    }
}
